package com.rainng.coursesystem.repository;

import com.rainng.coursesystem.model.vo.response.table.StudentExamItemVO;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentExamRepository {

    private final JdbcTemplate jdbcTemplate;

    public StudentExamRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    // 根据 studentId 查询考试信息
    public List<StudentExamItemVO> findExamsByStudentId(Integer studentId) {
        String sql = "SELECT sc.sc_id AS studentCourseId, " +
                "       c.course_name AS courseName, " +
                "       t.teacher_name AS teacherName, " +
                "       c.course_exam_date AS examDate, " +  // 修改为 c.course_exam_date
                "       c.course_exam_location AS examLocation " +  // 修改为 c.course_exam_location
                "FROM rc_student_course sc " +
                "JOIN rc_course c ON sc.sc_course_id = c.course_id " +
                "JOIN rc_teacher t ON c.course_teacher_id = t.teacher_id " +
                "WHERE sc.sc_student_id = ? " +
                "AND c.course_exam_date IS NOT NULL " +  // 修改为 c.course_exam_date
                "AND c.course_exam_location IS NOT NULL " +  // 修改为 c.course_exam_location
                "AND sc.sc_exam_score IS NULL";

        List<StudentExamItemVO> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(StudentExamItemVO.class), studentId);

        // 打印日志查看 SQL 查询结果
        System.out.println("查询结果: " + result);

        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(StudentExamItemVO.class), studentId);
    }
}
